package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.x.erp.model.Contract;
import com.x.erp.model.ContractRegion;
import com.x.erp.model.Region;
import com.x.erp.model.Shop;
import com.x.erp.model.vo.ContractVO;
import com.x.erp.rpc.ContractRegionRpcService;
import com.x.erp.rpc.ContractRpcService;
import com.x.erp.rpc.RegionRpcService;
import com.x.erp.util.ConstantState;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/contract")
public class ContractController extends BaseController {

	@Reference
	private ContractRpcService contractRpcService;//检测单信息服务

	@Reference
	private ContractRegionRpcService contractRegionRpcService;// 车辆服务

	@Reference
	private RegionRpcService regionRpcService;// 车辆服务


	/**
	 * 查询合同列表
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getContractList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getContractList(Contract contract, ContractVO contractVO, Shop shop) {
		contract.setShop(shop);
		contract.setContractVO(contractVO);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", contractRpcService.selectContractList(contract));
	}

	/**
	 * 查询合同数量
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getContractNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getContractNum(Contract contract, ContractVO contractVO, Shop shop) {
		contract.setShop(shop);
		contract.setContractVO(contractVO);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", contractRpcService.selectContractNum(contract));
	}


	/**
	 * 查询合同明细
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getContractRegionList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getContractRegionList(ContractRegion contractRegion) {
		List<Map<String, Object>> resultList = new ArrayList<>();
		Map<String, Object> res = null;
		List<ContractRegion> contractRegionList = contractRegionRpcService.selectContractRegionList(contractRegion);
		for (ContractRegion model : contractRegionList) {
			res = new HashMap<>();
			if (model != null) {
				res.put("regionId", model.getRegionId());
				res.put("regionNum", model.getRegion().getRegionNum());
				res.put("regionRestNum", model.getRegion().getRegionRestNum());
				res.put("regionName", model.getRegion().getRegionName());
				res.put("regionType", model.getRegion().getRegionType());
				res.put("contractRegionId", model.getContractRegionId());
				res.put("contractRegionRentFee", model.getContractRegionRentFee());
				res.put("contractRegionRentCount", model.getContractRegionRentCount());
				res.put("contractRegionAmount", model.getContractRegionAmount());
				res.put("contractRegionCarNo", model.getContractRegionCarNo());
				res.put("contractRegionParkCount", model.getContractRegionParkCount());
				res.put("contractRegionState", model.getContractRegionState());
			}
			resultList.add(res);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", resultList);
	}

	/**
	 * 新增合同及合同明细
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertContract", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertContract(Contract contract, String contractRegionList) {
		if (StringUtils.isEmpty(contract.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "marketId不能为空！");
		}
		if (StringUtils.isEmpty(contract.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "shopId不能为空！");
		}
		if (validateContractNo(contract)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "合同编号不能重复！");
		}
		contract.setContractCreateTime(this.getDate(0));
		contract.setContractUpdateTime(this.getDate(0));
		Contract contractInfo = contractRpcService.insertContract(contract);
		if (StringUtils.isNotEmpty(contractRegionList)) {
			List<ContractRegion> models = JSON.parseArray(contractRegionList, ContractRegion.class);
			for (ContractRegion item : models) {
				if (item.getContractRegionRentFee() == null) {
					return new ResultModel(ResultCode.RESULT_FAIL, "租金不能为空！");
				}
				if (item.getContractRegionRentCount() == null) {
					return new ResultModel(ResultCode.RESULT_FAIL, "面积或车位数不能为空！");
				}
				Region region = new Region();
				region.setRegionId(item.getRegionId());
				region.setMarketId(contractInfo.getMarketId());
				List<Region> regionInfoList = regionRpcService.selectRegionList(region);
				Region regionInfo = new Region();
				if (regionInfoList.size() > 0) {
					regionInfo = regionInfoList.get(0);
				}
				item.setRegion(regionInfo);
				item.setContractRegionCreateTime(this.getDate(0));
				item.setContractRegionUpdateTime(this.getDate(0));
				item.setContractId(contractInfo.getContractId());
				item.setContractRegionState(ConstantState.CONTRACT_REGION_STATE_1);
				contractRegionRpcService.insertContractRegion(item);
			}
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功");
	}


	/**
	 * 更新合同
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/updateContract", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateContract(Contract contract) {
		if (StringUtils.isEmpty(contract.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "shopId不能为空！");
		}
		if (validateContractNo(contract)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "合同编号不能重复！");
		}
		contract.setContractUpdateTime(this.getDate(0));
		contractRpcService.updateContract(contract);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "更新成功");
	}


	/**
	 * 新增合同明细
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertContractRegionInfo", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertContractRegionInfo(ContractRegion contractRegion, Region region) {
		if (region.getRegionRestNum() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "剩余不能为空！");
		}
		if (contractRegion.getContractRegionRentFee() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "租金不能为空！");
		}
		if (contractRegion.getContractRegionRentCount() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "面积或车位数不能为空！");
		}
		contractRegion.setContractRegionCreateTime(this.getDate(0));
		contractRegion.setContractRegionUpdateTime(this.getDate(0));
		contractRegion.setContractRegionState(ConstantState.CONTRACT_REGION_STATE_1);
		contractRegion.setRegion(region);
		contractRegionRpcService.insertContractRegion(contractRegion);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功");
	}

	/**
	 * 更新合同明细
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/updateContractRegionInfo", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateContractRegionInfo(ContractRegion contractRegion, Region region) {
		if (StringUtils.isEmpty(contractRegion.getContractRegionId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "合同明细ID不能为空！");
		}
		contractRegion.setContractRegionUpdateTime(this.getDate(0));
		contractRegion.setRegion(region);
		contractRegionRpcService.updateContractRegion(contractRegion);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "更新成功");
	}

	/**
	 * 删除合同明细
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/deleteContractRegion", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel deleteContractRegion(ContractRegion contractRegion, Region region) {
		if (StringUtils.isEmpty(contractRegion.getContractRegionId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "合同明细ID不能为空！");
		}
		contractRegion.setRegion(region);
		contractRegionRpcService.deleteContractRegion(contractRegion);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "更新成功");
	}


	/**
	 * 校验合同编号是否重复
	 *
	 * @param
	 * @return
	 */

	private boolean validateContractNo(Contract contract) {
		Contract contractInfo = new Contract();
		contractInfo.setContractNo(contract.getContractNo());
		contractInfo.setMarketId(contract.getMarketId());
		List<Contract> contractList = contractRpcService.selectContractList(contractInfo);
		if (contractList != null && contractList.size() > 0  && (StringUtils.isEmpty(contract.getContractId()) || !contractList.get(0).getContractId().equals(contract.getContractId()))){
			return true;
		}else{
			return false;
		}
	}
}